package yxy.game.pm2.command.hero;

import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.Soul;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.hero.HeroModule;
import yxy.game.pm2.module.resonate.ResonateHandle;

/**
 * 武将 武将重生确认
 */
@MessageType(proto = Command.HeroRebirthConfirm, classtype = HeroMessage.HeroRebirthConfirmRequest.class)
public class CMD_HeroRebirthConfirm extends Command<HeroMessage.HeroRebirthConfirmRequest> {

    @Override
    public void execute(MessageSource messageSource, HeroMessage.HeroRebirthConfirmRequest request) {
        String playerCode = messageSource.getTag();
        String code = request.getCode();

        HeroMessage.HeroRebirthConfirmResponse.Builder response = HeroMessage.HeroRebirthConfirmResponse.newBuilder();

        HeroModule heroModule = HeroModule.get(playerCode);

        try {
            if (Hero.lockHero(playerCode) && Soul.lockSoul(playerCode) && heroModule.tryLock()) {

                int maxtimes = fomula.武将重生每日使用次数.intValue();
                int times = maxtimes - heroModule.getRebirthTimes();

                //重生限制去除  前6次免费  后6次改为需要花费50元宝
//				if (times <= 0) {
//					send(messageSource, response, Result.HeroRebirthUpper);
//					return;
//				}

                HeroHandle heroHandle = new HeroHandle();
                Hero hero = heroHandle.getHero(playerCode, code);
                if (hero == null) {
                    send(messageSource, response, Result.ParamError);
                    return;
                }
                if (hero.getLevel() <= 1) {
                    send(messageSource, response, Result.ParamError);// 不能重生
                    return;
                }

                //共鸣的武将不能重生
                Result resultForResonate = ResonateHandle.heroIsResonateHero(hero, playerCode);
                if(!resultForResonate.equals(Result.Success)){
                    send(messageSource, response, resultForResonate);
                    return;
                }

//				long costNum = fomula.武将重生花费元宝.intValue(hero.getLevel());

                long costNum = times <= 0 ? 50 : 0;

                if (costNum > 0) {
                    Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costNum);
                    try {
                        new CurrencyHandle().cost(playerCode, CCReason.武将重生, cost);
                    } catch (CurrencyNotEnoughException e) {
                        send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
                        return;
                    }
                }
                Currencies back = new Currencies();

                // 卸下装备
                int[] equips = hero.getEquips();
                for (int i = 0; i < equips.length; i++) {
                    if (equips[i] > 0) {
                        back.add(Currency.create(CurrencyType.CT_装备, equips[i], 1));
                    }
                }

                back.add(hero.trainAll());

                // 返还培养
                new CurrencyHandle().addCurrency(playerCode, CCReason.武将重生, back);

                // 卸下魂玉
                if (hero.getSoulA() != null) {
                    Soul heroSoul = Soul.get(playerCode, hero.getSoulA());
                    if (heroSoul != null) {
                        hero.setSoulA(null);
                        heroSoul.setHero(null);
                        Soul.set(playerCode, heroSoul);
                        back.add(Currency.create(CurrencyType.CT_魂玉, heroSoul.getDataId(), 1));
                    }
                }
                if (hero.getSoulB() != null) {
                    Soul heroSoul = Soul.get(playerCode, hero.getSoulB());
                    if (heroSoul != null) {
                        hero.setSoulB(null);
                        heroSoul.setHero(null);
                        Soul.set(playerCode, heroSoul);
                        back.add(Currency.create(CurrencyType.CT_魂玉, heroSoul.getDataId(), 1));
                    }
                }

                // 武将还原
                hero.setEquips(null);
                hero.setLevel(1);
                hero.setStage(0);

                // 存档
                heroHandle.updateHero(playerCode, hero);

                heroModule.setRebirthTimes(heroModule.getRebirthTimes() + 1);
                heroModule.set();
                times = maxtimes - heroModule.getRebirthTimes() < 0 ? 0 : maxtimes - heroModule.getRebirthTimes();
                response.addAllBack(new CurrencyHandle().packItems(back));
                response.setHero(heroHandle.getHeroInfo(playerCode, hero));
                response.setTimes(times);

                send(messageSource, response, Result.Success);

                //判断如果是共鸣系统场上的武将，重生系列操作
                ResonateHandle.heroIsAboveHeroAndWeaken(hero, playerCode);

            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            Hero.unlockHero(playerCode);
            Soul.unlockSoul(playerCode);
            heroModule.unlock();
        }
    }

    private void send(MessageSource messageSource, HeroMessage.HeroRebirthConfirmResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}